<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="员工名称" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="部门名称" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作时间" prop="salaryTime">
        <el-date-picker clearable
          v-model="queryParams.salaryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择工作时间">
        </el-date-picker>
      </el-form-item>
       <el-form-item label="出勤天数" prop="attendanceDays">
        <el-input
          v-model="queryParams.attendanceDays"
          placeholder="请输入出勤天数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:salary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:salary:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:salary:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleCustomExport"
          v-hasPermi="['module:salary:export']"
        >自定义导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>
    <el-dialog title="自定义导出" :visible.sync="customOpen" width="500px" append-to-body>
      <el-checkbox-group v-model="checkList" class="checkBox">
        <el-checkbox label="staffName" class="label">员工名称</el-checkbox>
        <el-checkbox label="postName" class="label">岗位</el-checkbox>
        <el-checkbox label="salaryTime" class="label">工作时间</el-checkbox>
        <el-checkbox label="entryTime" class="label">入职日期</el-checkbox>
        <el-checkbox label="attendanceDays" class="label">出勤天数</el-checkbox>
        <el-checkbox label="baseSalary" class="label">基本工资</el-checkbox>
        <el-checkbox label="postSubsidy" class="label">岗位津贴</el-checkbox>
        <el-checkbox label="senioritySalary" class="label">工龄工资</el-checkbox>
        <el-checkbox label="skillsAssessSalary" class="label">技能考核工资</el-checkbox>
        <el-checkbox label="manageAssessSalary" class="label">管理考核工资</el-checkbox>
        <el-checkbox label="safetyAward" class="label">安全奖</el-checkbox>
        <el-checkbox label="meritSalary" class="label">绩效工资</el-checkbox>
        <el-checkbox label="fullTimeSalary" class="label">全勤奖</el-checkbox>
        <el-checkbox label="attendanceSalary" class="label">出勤工资</el-checkbox>、
        <el-checkbox label="specialAssess" class="label">专项考核</el-checkbox>
        <el-checkbox label="wagesPayable" class="label">应发工资</el-checkbox>
        <el-checkbox label="socialSecurity" class="label">社保</el-checkbox>
        <el-checkbox label="personalIncomeTax" class="label">个人所得税</el-checkbox>
        <el-checkbox label="forfeit" class="label">罚款</el-checkbox>
        <el-checkbox label="other" class="label">其他</el-checkbox>
        <el-checkbox label="netSalary" class="label">实发工资</el-checkbox>
        <el-checkbox label="salaryInformation" class="label">工资信息</el-checkbox>
        <el-checkbox label="remark" class="label">备注</el-checkbox>

      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="getinfo">确 定</el-button>
                <el-button @click="cancelCustomExport">取 消</el-button>
      </div>
    </el-dialog>

    <el-table :border="true" v-loading="loading" :data="salaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="员工名称" align="center" prop="staffName" v-if="columns[0].visible"/>
      <el-table-column label="岗位" align="center" prop="postName" v-if="columns[1].visible"/>
      <el-table-column label="工作时间" align="center" prop="salaryTime" width="180" v-if="columns[2].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.salaryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职日期" align="center" prop="entryTime" width="180" v-if="columns[3].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出勤天数" align="center" prop="attendanceDays" v-if="columns[4].visible"/>
      <el-table-column label="基本工资" align="center" prop="baseSalary" v-if="columns[5].visible"/>
      <el-table-column label="岗位津贴" align="center" prop="postSubsidy" v-if="columns[6].visible"/>
      <el-table-column label="工龄工资" align="center" prop="senioritySalary" v-if="columns[7].visible"/>
      <el-table-column label="技能考核工资" align="center" prop="skillsAssessSalary" v-if="columns[8].visible"/>
      <el-table-column label="管理考核工资" align="center" prop="manageAssessSalary" v-if="columns[9].visible"/>
      <el-table-column label="安全奖" align="center" prop="safetyAward" v-if="columns[10].visible"/>
      <el-table-column label="绩效工资" align="center" prop="meritSalary" v-if="columns[11].visible"/>
      <el-table-column label="全勤奖" align="center" prop="fullTimeSalary" v-if="columns[12].visible"/>
      <el-table-column label="出勤工资" align="center" prop="attendanceSalary" v-if="columns[13].visible"/>
      <el-table-column label="专项考核" align="center" prop="specialAssess" v-if="columns[14].visible"/>
      <el-table-column label="应发工资" align="center" prop="wagesPayable" v-if="columns[15].visible"/>
      <el-table-column label="社保" align="center" prop="socialSecurity" v-if="columns[16].visible"/>
      <el-table-column label="个人所得税" align="center" prop="personalIncomeTax" v-if="columns[17].visible"/>
      <el-table-column label="罚款" align="center" prop="forfeit" v-if="columns[18].visible"/>
      <el-table-column label="其他" align="center" prop="other" v-if="columns[19].visible"/>
      <el-table-column label="实发工资" align="center" prop="netSalary" v-if="columns[20].visible"/>
      <el-table-column label="工资信息" align="center" prop="salaryInformation" v-if="columns[21].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[22].visible"/>
      <el-table-column label="操作"  fixed="right" width="120px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:salary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:salary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改薪资对话框 -->
    <el-dialog :title="title"   :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="180px">
<!--        <el-form-item label="员工id" prop="staffId">-->
<!--          <el-input v-model="form.staffId" placeholder="请输入员工id" />-->
<!--        </el-form-item>-->
        <el-form-item label="员工名称" prop="staffName">
          <el-input :disabled="true" style="background-color:#fff;" v-model="form.staffName" placeholder="请输入员工名称" />
        </el-form-item>
<!--        <el-form-item label="部门id" prop="postId">-->
<!--          <el-input v-model="form.postId" placeholder="请输入部门id" />-->
<!--        </el-form-item>-->
        <el-form-item label="岗位" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item style="width: 250px;" label="归属部门" prop="deptId">
          <treeselect v-model="form.deptId"  :options="deptOption" :show-count="true" placeholder="请选择归属部门"/>
        </el-form-item>

        <el-form-item label="工作时间" prop="salaryTime">
          <el-date-picker clearable
            v-model="form.salaryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择工资时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入职日期" prop="entryTime">
          <el-date-picker clearable
            v-model="form.entryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出勤天数" prop="attendanceDays">
          <el-input v-model="form.attendanceDays" placeholder="请输入出勤天数" />
        </el-form-item>
        <el-form-item label="基本工资" prop="baseSalary">
          <el-input v-model="form.baseSalary" placeholder="请输入基本工资" />
        </el-form-item>
        <el-form-item label="岗位津贴" prop="postSubsidy">
          <el-input v-model="form.postSubsidy" placeholder="请输入岗位津贴" />
        </el-form-item>
        <el-form-item label="工龄工资" prop="senioritySalary">
          <el-input v-model="form.senioritySalary" placeholder="请输入工龄工资" />
        </el-form-item>
        <el-form-item label="技能考核工资" prop="skillsAssessSalary">
          <el-input v-model="form.skillsAssessSalary" placeholder="请输入技能考核工资" />
        </el-form-item>
        <el-form-item label="管理考核工资" prop="manageAssessSalary">
          <el-input v-model="form.manageAssessSalary" placeholder="请输入管理考核工资" />
        </el-form-item>
        <el-form-item label="安全奖" prop="safetyAward">
          <el-input v-model="form.safetyAward" placeholder="请输入安全奖" />
        </el-form-item>
        <el-form-item label="绩效工资" prop="meritSalary">
          <el-input v-model="form.meritSalary" placeholder="请输入绩效工资" />
        </el-form-item>
        <el-form-item label="全勤奖" prop="fullTimeSalary">
          <el-input v-model="form.fullTimeSalary" placeholder="请输入全勤工资" />
        </el-form-item>
        <el-form-item label="出勤工资" prop="attendanceSalary">
          <el-input v-model="form.attendanceSalary" placeholder="请输入出勤工资" />
        </el-form-item>
        <el-form-item label="专项考核" prop="specialAssess">
          <el-input v-model="form.specialAssess" placeholder="请输入专项考核" />
        </el-form-item>
        <el-form-item label="应发工资" prop="wagesPayable">
          <el-input v-model="form.wagesPayable" placeholder="请输入应发工资" />
        </el-form-item>
        <el-form-item label="社保" prop="socialSecurity">
          <el-input v-model="form.socialSecurity" placeholder="请输入社保" />
        </el-form-item>
        <el-form-item label="个人所得税" prop="personalIncomeTax">
          <el-input v-model="form.personalIncomeTax" placeholder="请输入个人所得税" />
        </el-form-item>
        <el-form-item label="罚款" prop="forfeit">
          <el-input v-model="form.forfeit" placeholder="请输入罚款" />
        </el-form-item>
        <el-form-item label="其他" prop="other">
          <el-input v-model="form.other" placeholder="请输入其他" />
        </el-form-item>
        <el-form-item label="实发工资" prop="netSalary">
          <el-input v-model="form.netSalary" placeholder="请输入实发工资" />
        </el-form-item>
        <el-form-item label="工资信息" prop="salaryInformation">
          <el-input v-model="form.salaryInformation" placeholder="请输入工资信息" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listSalary, getSalary, delSalary, addSalary, updateSalary,countSalary,exportSalaryApi} from "@/api/module/salary";
import {deptTreeSelect} from '@/api/system/user.js'
import {exportFile} from '../common/common'

export default {
  name: "Salary",
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪资表格数据
      salaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      customOpen:false,
      checkList:[],
      fields:{
        fieldsNames:''
      },//自定义导出
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffId: null,
        staffName: null,
        postId: null,
        postName: null,
        salaryTime: null,
        entryTime: null,
        attendanceDays: null,
        baseSalary: null,
        postSubsidy: null,
        senioritySalary: null,
        skillsAssessSalary: null,
        manageAssessSalary: null,
        safetyAward: null,
        meritSalary: null,
        fullTimeSalary: null,
        attendanceSalary: null,
        specialAssess: null,
        wagesPayable: null,
        socialSecurity: null,
        personalIncomeTax: null,
        forfeit: null,
        other: null,
        netSalary: null,
        salaryInformation: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        { key: 0, label: `员工名称`, visible: true },
        { key: 1, label: `岗位`, visible: true },
        { key: 2, label: `工作时间`, visible: true },
        { key: 3, label: `入职日期`, visible: true },
        { key: 4, label: `出勤天数`, visible: true },
        { key: 5, label: `基本工资`, visible: true },
        { key: 6, label: `岗位津贴`, visible: true },
        { key: 7, label: `工龄工资`, visible: true },
        { key: 8, label: `技能考核工资`, visible: true },
        { key: 9, label: `管理考核工资`, visible: true },
        { key: 10, label: `安全奖`, visible: true },
        { key: 11, label: `绩效工资`, visible: true },
        { key: 12, label: `全勤工资`, visible: true },
        { key: 13, label: `出勤工资`, visible: true },
        { key: 14, label: `专项考核`, visible: true },
        { key: 15, label: `应发工资`, visible: true },
        { key: 16, label: `社保`, visible: true },
        { key: 17, label: `个人所得税`, visible: true },
        { key: 18, label: `罚款`, visible: true },
        { key: 19, label: `其他`, visible: true },
        { key: 20, label: `实发工资`, visible: true },
        { key: 21, label: `工资信息`, visible: true },
        { key: 22, label: `备注`, visible: true },

      ],
      // 表单校验
      rules: {
      },
      deptOption:[],//部门
    };
  },
  created() {
    this.getList();
    this.getDept()
  },
  methods: {
    /** 自定义导出 */
    handleCustomExport(){
      this.customOpen=true
    },
    getinfo(){
      this.fields.fieldsNames= this.checkList.join()
      //{fieldsNames:'username,applicationType'}
      exportSalaryApi({fieldsNames: this.fields.fieldsNames}).then((res)=>{
        const fileName = '薪资报表'
        const contentType =
          'application/vnd.ms-excel'
        exportFile(res, contentType, fileName)
      })
        .catch((err) => {
          console.log(err)
        })
      this.checkList=[]
      this.customOpen=false
    },
    cancelCustomExport(){
      this.checkList=[]
      this.customOpen=false
    },
    /** 查询薪资列表 */
    getList() {
      this.loading = true;
      listSalary(this.queryParams).then(response => {
        this.salaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    //deptTreeSelect 获取部门
    getDept(){
      deptTreeSelect().then(res=>{
        console.log(res)
        this.deptOption=res.data
      })

    },

    //选择部门
    handleChange(e){
      console.log(e)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staffId: null,
        staffName: null,
        postId: null,
        postName: null,
        salaryTime: null,
        entryTime: null,
        attendanceDays: null,
        baseSalary: null,
        postSubsidy: null,
        senioritySalary: null,
        skillsAssessSalary: null,
        manageAssessSalary: null,
        safetyAward: null,
        meritSalary: null,
        fullTimeSalary: null,
        attendanceSalary: null,
        specialAssess: null,
        wagesPayable: null,
        socialSecurity: null,
        personalIncomeTax: null,
        forfeit: null,
        other: null,
        netSalary: null,
        salaryInformation: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加薪资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSalary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改薪资";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSalary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            countSalary(this.form).then(res=>{
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            addSalary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除薪资编号为"' + ids + '"的数据项？').then(function() {
        return delSalary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/salary/export', {
        ...this.queryParams
      }, `salary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.checkBox{
  display: flex;
  flex-wrap: wrap;
}
.checkBox .label{
  width: 25%;
  height: 30px;
  /*background-color: red;*/
}
</style>
